package collection.exercise;

import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-02-02
 * Time:17:31
 */
@SuppressWarnings("all")
public class Exercise3 {
    public static void main(String[] args) {
        List<Book1> book1s = new LinkedList<>();
        book1s.add(new Book1("红楼梦", "曹雪芹", 100));
        book1s.add(new Book1("西游记", "吴承恩", 10));
        book1s.add(new Book1("水浒传", "施耐庵", 9));
        book1s.add(new Book1("三国演义", "罗贯中", 80));
        book1s.add(new Book1("西游记", "吴承恩", 10));
        bubbleSort(book1s);
        for (Book1 book1 : book1s) {
            System.out.println(book1);
        }
    }

    public static void main2(String[] args) {
        List<Book1> book1s = new Vector<>();
        book1s.add(new Book1("红楼梦", "曹雪芹", 100));
        book1s.add(new Book1("西游记", "吴承恩", 10));
        book1s.add(new Book1("水浒传", "施耐庵", 9));
        book1s.add(new Book1("三国演义", "罗贯中", 80));
        book1s.add(new Book1("西游记", "吴承恩", 10));
        bubbleSort(book1s);
        for (Book1 book1 : book1s) {
            System.out.println(book1);
        }
    }

    public static void main1(String[] args) {
        List<Book1> book1s = new ArrayList<>();
        book1s.add(new Book1("红楼梦", "曹雪芹", 100));
        book1s.add(new Book1("西游记", "吴承恩", 10));
        book1s.add(new Book1("水浒传", "施耐庵", 9));
        book1s.add(new Book1("三国演义", "罗贯中", 80));
        book1s.add(new Book1("西游记", "吴承恩", 10));
        bubbleSort(book1s);
        for (Book1 book1 : book1s) {
            System.out.println(book1);
        }
    }

    public static void bubbleSort(List<Book1> book1s) {
        for (int i = 0; i < book1s.size() - 1; i++) {
            for (int j = 0; j < book1s.size() - i - 1; j++) {
                if (compare(book1s.get(j), book1s.get(j + 1))) {
                    Book1 tmpBook = book1s.get(j);
                    book1s.set(j, book1s.get(j + 1));
                    book1s.set(j + 1, tmpBook);
                }
            }
        }
    }

    public static boolean compare(Book1 b1, Book1 b2) {
        return b1.getPrice() - b2.getPrice() > 0;
    }
}

class Book1 {
    String name;
    String author;
    int price;

    public Book1(String name, String author, int price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "名称:" + name + "\t价格:" + price + "\t作者:" + author;
    }

}